//
//  WXPOrderDetailVC.m
//  MexicoLoan
//
//  Created by WXP on 2025/3/10.
//
//

#import "WXPOrderDetailVC.h"
#import "WXPOrderDetailHeaderView.h"
#import "WXPOrderDetailFooterView.h"
#import "WXPOrderDeferDetailVC.h"
#import "WXPNormalRepaymentVC.h"
#import "AutoAgainLoanPopUpView.h"

@interface WXPOrderDetailVC ()

@property (nonatomic, strong) WXPNavView * navView;

@property (nonatomic, strong) WXPCommonAgreementView * agreementView;
@property (nonatomic, strong) WXPOrderDetailHeaderView * headerView;
@property (nonatomic, strong) WXPOrderDetailFooterView * footerView;
@property (nonatomic, strong) ModelOrderDetail * orderDetailModel;
@property (nonatomic, strong) AutoAgainLoanPopUpView * autoAgainLoanPopUpView;

@end

@implementation WXPOrderDetailVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (_navView == nil) {
        _navView = [WXPNavView  new];
        [_navView resetViewWithTitle:@"Detalles del préstamo"];
        _navView.leftTop = XY(0, 0);
    }
    return  _navView;
}
- (WXPCommonAgreementView *)agreementView{
    if (!_agreementView) {
        _agreementView = [WXPCommonAgreementView new];
        _agreementView.centerXBottom = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT-LEFT_MARDIN);
    }
    return _agreementView;
}
- (WXPOrderDetailHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [WXPOrderDetailHeaderView new];
        WEAKSELF
        _headerView.blockRepayment = ^{
            [weakSelf requestShowAutoApply];
        };
        _headerView.blockDeferRepayment = ^{
            WXPOrderDeferDetailVC * vc = [WXPOrderDeferDetailVC new];
            vc.orderDetailModel = weakSelf.orderDetailModel;
            [Global_Nav pushViewController:vc animated:true];
        };
    }
    return _headerView;
}
- (WXPOrderDetailFooterView *)footerView{
    if (!_footerView) {
        _footerView = [WXPOrderDetailFooterView new];
    }
    return _footerView;
}
- (AutoAgainLoanPopUpView *)autoAgainLoanPopUpView{
    if (!_autoAgainLoanPopUpView) {
        _autoAgainLoanPopUpView = [AutoAgainLoanPopUpView new];
        WEAKSELF
        _autoAgainLoanPopUpView.blockConfirmClick = ^{
            [GlobalMethod hitPointWithPageName:KEY_AutoReloan2Dialog action:KEY_Click extInfo:@{@"loanOrderId": weakSelf.orderModel.orderNo, @"open": weakSelf.autoAgainLoanPopUpView.btnAgreement.selected == true ? @"true" : @"false"}];
            [weakSelf requestSettledAutoApply];
        };
        _autoAgainLoanPopUpView.blockAgreementClick = ^(BOOL isSelect) {
            [GlobalMethod hitPointWithPageName:KEY_AutoReloan2Dialog action:KEY_Auto_Reloan2_Switch_Click extInfo:@{@"loanOrderId": weakSelf.orderModel.orderNo, @"type": isSelect == true ? @"true" : @"false"}];
        };
    }
    return _autoAgainLoanPopUpView;
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.hidden = true;
    [self configUI];
    [self requestDetail];
    [self requestBankCardList];
}

- (void)configUI{
    self.view.backgroundColor = COLOR_BACKGROUND;
    self.viewBG.backgroundColor = COLOR_BACKGROUND;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableHeaderTopBg.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.navView];
    [self.view addSubview:self.agreementView];
    self.tableView.height = self.agreementView.top-self.navView.bottom-10;
    self.tableView.top = self.navView.bottom+10;
    self.tableView.tableHeaderView = self.headerView;
    self.tableView.tableFooterView = self.footerView;
}

#pragma mark - request
- (void)requestDetail{
    [self showLoadingView];
    // 打点
    NSString * typeStr = @"";
    if(self.orderModel.status.integerValue == 170){
        typeStr = @"TO_REPAID";
    }else if(self.orderModel.status.integerValue == 180){
        typeStr = @"OVERDUE";
    }else if(self.orderModel.status.integerValue == 90 || self.orderModel.status.integerValue == 100){
        typeStr = @"UNDER_REVIEW";
    }else if(self.orderModel.status.integerValue == 150){
        typeStr = @"LENDING";
    }else if(self.orderModel.status.integerValue == 200){
        typeStr = @"REPAID";
    }
    [GlobalMethod hitPointWithPageName:KEY_LoanStatus action:KEY_Enter extInfo:@{@"type": typeStr, @"loanOrderId": self.orderModel.orderNo}];
    
    NSString * type = @"1";
    if(self.orderModel.status.integerValue == 170 || self.orderModel.status.integerValue == 180){
        // 审核中、放款中、已还传1，待还款、逾期传2
        type = @"2";
    }
    [RequestApi requestOrderDetailWithPageType:type orderNo:self.orderModel.orderNo delegate:nil success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        self.orderDetailModel = [GlobalMethod exchangeDicToModel:response modelName:@"ModelOrderDetail"];
        [self.headerView resetView:self.orderDetailModel];
        self.tableView.tableHeaderView = self.headerView;
  
        NSMutableArray * pairsArray = [NSMutableArray array];
        // 待还款、逾期订单，列表需要单独增加一条，展示在第一条，左边为Fecha de vencimiento，右边取expiryTime字段
        if(self.orderModel.status.integerValue == 170 || self.orderModel.status.integerValue == 180){
            ModelPairs * model = [ModelPairs new];
            model.key = @"Fecha de vencimiento";
            model.value = self.orderDetailModel.expiryTime;
            [pairsArray addObject:model];
        }
        [pairsArray addObjectsFromArray:self.orderDetailModel.pairs];
        [self.footerView resetView:pairsArray];
        self.tableView.tableFooterView = self.footerView;
        self.tableView.hidden = false;
        [self.loadingView hideLoading];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

// 银行卡列表
- (void)requestBankCardList{
    [RequestApi requestBankCardListWithDelegate:nil success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        NSArray * aryResponse = [GlobalMethod exchangeDic:response toAryWithModelName:@"ModelBankCard"];
        if (isAry(aryResponse)){
            ModelBankCard * model = aryResponse.firstObject;
            NSDictionary * dataDic = [GlobalMethod exchangeStringToDic:readStr(model.data)];
            self.autoAgainLoanPopUpView.bankName = [dataDic stringValueForKey:@"bankName"];
            self.autoAgainLoanPopUpView.bankCard = [dataDic stringValueForKey:@"bankCard"];
        }
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

// 是否展示自动复借
- (void)requestShowAutoApply{
    [RequestApi requestShowAutoApplyWithDelegate:self success:^(id _Nonnull response, id  _Nonnull mark) {
        BOOL isShow = false;
        if ([response isKindOfClass:[NSNumber class]]) {
            isShow = [response boolValue];
        }
        [GlobalMethod hitPointWithPageName:KEY_AutoReloan2Dialog action:KEY_Auto_Reloan2_Show extInfo:@{@"loanOrderId": self.orderModel.orderNo, @"show": isShow == true ? @"true" : @"false"}];
        if (isShow == true) {
            // 展示复借弹窗
            UIWindow * window = [[[UIApplication sharedApplication] delegate] window];
            [self.autoAgainLoanPopUpView resetView:self.orderDetailModel];
            [window addSubview:self.autoAgainLoanPopUpView];
        }else{
            WXPNormalRepaymentVC * vc = [WXPNormalRepaymentVC new];
            vc.orderDetailModel = self.orderDetailModel;
            [Global_Nav pushViewController:vc animated:true];
        }
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
        
    }];
}

// 是否自动复借
- (void)requestSettledAutoApply{
    NSString * type = self.autoAgainLoanPopUpView.btnAgreement.selected == true ? @"true" : @"false";
    [RequestApi requestSettledAutoApplyWithOrderNo:self.orderModel.orderNo type:type delegate:self success:^(NSDictionary * _Nonnull response, id  _Nonnull mark) {
        
        [self.autoAgainLoanPopUpView removeFromSuperview];
        WXPNormalRepaymentVC * vc = [WXPNormalRepaymentVC new];
        vc.orderDetailModel = self.orderDetailModel;
        [Global_Nav pushViewController:vc animated:true];
        
    } failure:^(NSString * _Nonnull errorStr, id  _Nonnull mark) {
            
    }];
}




@end
